﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace TimeZoneConverter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // timer for updating current time
        DispatcherTimer _timer;
        TimeZoneInfo localTimeZone;

        public MainWindow()
        {
            InitializeComponent();

            srcTime.Text = DateTime.Now.ToLongTimeString();

            // load time zones
            foreach (var tz in TimeZoneInfo.GetSystemTimeZones())
            {
                targetTimeZone.Items.Add(tz);
                if (tz.DisplayName.Contains("Tashkent"))
                    targetTimeZone.SelectedItem = tz;

                srcTimeZone.Items.Add(tz);
                if (tz.BaseUtcOffset == TimeZoneInfo.Local.BaseUtcOffset)
                    srcTimeZone.SelectedItem = localTimeZone = tz;
            }

            // setup a timer for updating current time
            _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
            _timer.Tick += delegate
            {
                UpdateInputTime();
            };
        }

        /// <summary>
        /// set sourceTime.Text to current time
        /// </summary>
        private void UpdateInputTime()
        {
            var timeZone = (TimeZoneInfo)srcTimeZone.SelectedItem;
            srcTime.Text = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZone).ToLongTimeString();
        }
        /// <summary>
        /// Update targetTime.Text based on source time, time zonea and target time zone
        /// </summary>
        private void UpdateOutputTime()
        {

            DateTime sourceTime;
            if (!DateTime.TryParse(srcTime.Text, out sourceTime))
                srcTime.Background = Brushes.LightPink;
            else
            {
                srcTime.Background = SystemColors.WindowBrush;

                var destTimeZone = (TimeZoneInfo)this.targetTimeZone.SelectedItem;
                var sourceTimeZone = (TimeZoneInfo)this.srcTimeZone.SelectedItem;

                if (destTimeZone == null || sourceTimeZone == null)
                    return;
                targetTime.Text = TimeZoneInfo.ConvertTime(sourceTime, sourceTimeZone, destTimeZone).ToLongTimeString();
            }
        }

        /// <summary>
        /// update target time when source time is changed
        /// </summary>
        private void input_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateOutputTime();
        }

        /// <summary>
        /// update target time when a time zone is changed
        /// </summary>
        private void OnTimeZoneChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateOutputTime();
        }

        /// <summary>
        /// Enable the timer and disable editing when "use current" is checked
        /// </summary>
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            UpdateInputTime();
            _timer.IsEnabled = useCurrentTime.IsChecked != false;
            srcTimeZone.IsEnabled = srcTime.IsEnabled = useCurrentTime.IsChecked == false;

            if (useCurrentTime.IsChecked == true)
                srcTimeZone.SelectedItem = localTimeZone;
        }

    }
}
